﻿namespace PokerSimulator
{
	using System.Collections.Generic;
	using System.Linq;

	public class GameTally
	{
		private readonly int[] _handMap = new int[10];
		private readonly Dictionary<double, long> _store = new Dictionary<double, long>();

		public IEnumerable<double> WinningValues
		{
			get { return _store.Keys; }
		}

		public void Add(double key)
		{
			int integerKeyValue = (int)key;
			_handMap[MapValueToHandSlot(integerKeyValue)]++;

			long val;
			if (_store.TryGetValue(key, out val))
			{
				val++;
				_store[key] = val;
			}
			else
			{
				_store.Add(key, 1);
			}
		}

		private static int MapValueToHandSlot(int handValue)
		{
			if (handValue >= HandValue.Minimum.StraightFlush)
			{
				return 0;
			}
			if (handValue >= HandValue.Minimum.FourOfAKind)
			{
				return 1;
			}
			if (handValue >= HandValue.Minimum.FullHouse)
			{
				return 2;
			}
			if (handValue >= HandValue.Minimum.Flush)
			{
				return 3;
			}
			if (handValue >= HandValue.Minimum.Straight)
			{
				return 4;
			}
			if (handValue >= HandValue.Minimum.ThreeOfAKind)
			{
				return 5;
			}
			if (handValue >= HandValue.Minimum.TwoPair)
			{
				return 6;
			}
			if (handValue >= HandValue.Minimum.Pair)
			{
				return 7;
			}
			if (handValue >= HandValue.Minimum.HighCard)
			{
				return 8;
			}
			if (handValue == 0)
			{
				return 9;
			}
			return -1;
		}

		internal long GetTotal()
		{
			return _handMap.Sum();
		}

		internal int GetStraightFlushCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.StraightFlush)];
		}

		internal int Get4KindCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.FourOfAKind)];
		}

		internal int GetFullHouseCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.FullHouse)];
		}

		internal int GetFlushCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.Flush)];
		}

		internal int GetStraightCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.Straight)];
		}

		internal int Get3KindCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.ThreeOfAKind)];
		}

		internal int Get2PairCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.TwoPair)];
		}

		internal int GetHighCardCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.HighCard)];
		}

		internal int GetAllFoldedCount()
		{
			return _handMap[MapValueToHandSlot(HandValue.Minimum.Fold)];
		}
	}
}